package com.lanou.dllo.findfood.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;

import com.lanou.dllo.findfood.activity.MenuDescipetinActivity;
import com.lanou.dllo.findfood.adpter.LoseRecylerAdpter;
import com.lanou.dllo.findfood.R;
import com.lanou.dllo.findfood.database.MenuLoseWeightDataBaseUtil;
import com.lanou.dllo.findfood.dialog.CookProgressDialog;
import com.lanou.dllo.findfood.entity.Menu;
import com.lanou.dllo.findfood.entity.MenuResult;
import com.lanou.dllo.findfood.util.Menuimage;
import com.lanou.dllo.findfood.volley.VolleySington;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dllo on 15/10/23.
 */
public class MenuLoseWightFragment extends Fragment implements Menuimage{
    private RecyclerView recyclerView;
    private LoseRecylerAdpter adpter;
    private MenuLoseWeightDataBaseUtil dataBaseUtil;


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


        View view =  inflater.inflate(R.layout.fragment_losewight,null);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
       requestData();


    }
 private void    initView(){
    // dataBaseUtil = new MenuLoseWeightDataBaseUtil(getContext());
        recyclerView = (RecyclerView) getActivity().findViewById(R.id.loseReclerview);
     GridLayoutManager manager =new  GridLayoutManager(getActivity(),1);
     adpter = new LoseRecylerAdpter();
     recyclerView.setLayoutManager(manager);
     recyclerView.setAdapter(adpter);
     adpter.setImageListener(this);



    }
    public void requestData() {
       final CookProgressDialog anmition = new CookProgressDialog(getContext(),R.id.animaImg);
        anmition.show();
        String url = "http://apis.baidu.com/tngou/cook/list";
        String httpArg = "id=10&page=1&rows=20";
        String path = url + "?" + httpArg;
        StringRequest request = new StringRequest(path, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                Menu menu = gson.fromJson(response, Menu.class);
                if (menu != null) {
                    List<MenuResult> results = menu.getTngou();
                    if (results != null && results.size() > 0) {
                        adpter.addData(results);
                     //  dataBaseUtil.saveData(results);


                    }
                }
anmition.dismiss();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        }) {
            @Override
            //在StringRespset结束之前写{}在大括号里面写 getHeaders() 自动重写方法
            //用Map方法键值对形势添加
            //添加请求头
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                map.put("apikey", "7df89e2ed7c7b92e1a2fe5ec8036d0cd");
                return map;
            }
        };
        VolleySington.getInstance().addRequest(request);


    }


    @Override
    public void MenuimageOnclick(MenuResult result) {
        Intent intent = new Intent(getActivity(), MenuDescipetinActivity.class);
        intent.putExtra("id", result.getId());
        startActivity(intent);
    }
}
